<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 货运单结算
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button type="info"
                   plain
                   id="nopoint">请输入货运单编号</el-button>
        <el-input v-model="query.waybillNumber"
                  placeholder="货运单编号"
                  class="handle-input mr10  selectValue"></el-input>

        <el-button type="info"
                   plain
                   id="nopoint">客户姓名</el-button>
        <el-input v-model="query.customerName"
                  placeholder="发货/收货客户姓名"
                  class="handle-input mr10 selectValue"></el-input>

        <!-- <el-button type="info"
                   plain
                   id="nopoint">发货时间</el-button>
        <el-date-picker v-model="value1"
                        type="datetimerange"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['8:00:00','8:00:00']"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        style="width:334px"
                        class="selectValue">
        </el-date-picker> -->

        <el-button type="primary"
                   icon="el-icon-search"
                   @click="handleSearch"
                   class="select-button">搜索</el-button>
        <el-button type="primary"
                   icon="el-icon-search"
                   @click="handleClear">重置</el-button>
        <!-- <el-button type="primary"
                   icon="el-icon-delete"
                   class="handle-del mr10"
                   @click="delAllSelection">批量删除</el-button> -->

      </div>
      <el-table :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection"
                         width="55"
                         align="center"></el-table-column>
        <el-table-column prop="waybillNumber"
                         label="货运单编号"
                         width="300"
                         align="center"
                         fixed></el-table-column>
        <el-table-column prop="consignorName"
                         label="发货客户"></el-table-column>
        <el-table-column prop="consigneeName"
                         label="收货客户"></el-table-column>
        <!-- <el-table-column label="减款(元)"
                         align="center">
          <template slot-scope="scope">
            <el-image class="table-td-thumb"
                      :src="scope.row.thumb"
                      :preview-src-list="[scope.row.thumb]"></el-image>
          </template>
        </el-table-column> -->
        <el-table-column prop="refundType"
                         label="业务类型"
                         :formatter="stateFormat"></el-table-column>
        <el-table-column prop="rebates"
                         label="应收贷款(元)"></el-table-column>
        <el-table-column prop=""
                         label="变更后金额(元)">无变更</el-table-column>
        <el-table-column prop="status"
                         label="是否审核"
                         :formatter="stateFormat1"></el-table-column>
        <el-table-column prop="status"
                         label="是否结算"
                         :formatter="stateFormat2"></el-table-column>

        <el-table-column label="操作"
                         width="180"
                         align="center"
                         fixed="right">
          <template slot-scope="scope">
            <el-button type="primary"
                       icon="el-icon-edit"
                       @click="handleEdit(scope.$index, scope.row)">业务</el-button>
            <!-- <el-button type="danger"
                       icon="el-icon-delete"
                       @click="handleDelete(scope.$index, scope.row)"
                       >取消业务</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <!-- <el-pagination background
                       layout="total, prev, pager, next"
                       :current-page="query.pageIndex"
                       :page-size="query.pageSize"
                       :total="pageTotal"
                       @current-change="handlePageChange"></el-pagination> -->

        <el-pagination @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page.sync="currentPage2"
                       background
                       :page-sizes="[3, 5, 10, 20]"
                       :page-size="5"
                       layout="total,sizes, prev, pager, next"
                       :total="total">
        </el-pagination>

      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑"
               :visible.sync="editVisible"
               width="30%">
      <el-form ref="form"
               :model="form"
               label-width="70px">
        <el-form-item label="货运单编号">
          <el-input v-model="form.waybillNumber"></el-input>
        </el-form-item>
        <el-form-item label="客户名称">
          <el-input v-model="form.consignorName"></el-input>
        </el-form-item>
        <el-form-item label="货款变更">
          <el-input v-model="form.rebates"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
           <el-switch
            v-model="form.refundType"
            active-text="单日业务"
            inactive-text="无业务"
            active-value="1"
            inactive-value="0">
          </el-switch>
        </el-form-item> 
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchData } from '../../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                currentPage: '1',
                pageSize: '5',
                waybillNumber: '',
                //客户姓名
                customerName: '',
                //开始时间
                startTime: '',
                //结束时间
                endTime: ''
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {

            },

            idx: -1,
            id: -1,
            // 时间日期数据value1
            value1: '',
            //当前页码
            currentPage2: 1,
            total: 10,
            settleData: {
                //预付金额
                prepaymentAmount: 0,
                //本单金额
                price: 0,
                //应收金额
                amountReceivable: 0,
                //已收金额
                amountReceived: 0,
                //发送后端货运单参数
                waybillNumber: '',
                freight: '',
                rabates: '',
                premium: '',
                moveCharges: '',
                settlementDate: '',
                refundType: ''
            },
            changestatus:'0'
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.query.startTime = this.value1[0];
            this.query.endTime = this.value1[1];
            this.$axios.post('/api/settlement/business/findAll', this.query).then((res) => {
                console.log(res);
                this.tableData = res.data.data.list;
                this.total = res.data.data.total;
                if (res.data.code == 200) {
                    this.$message.success('货运单查询成功');
                } else {
                    this.$message.error('货运单查询失败');
                }
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            console.log(this.value1);
            this.getData();
        },
        // 清除搜索框输入
        handleClear() {
            (this.query.waybillNumber = ''), (this.query.customerName = ''), (this.value1 = '');
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    const length = this.multipleSelection.length;
                    let str = '';
                    this.delList = this.delList.concat(this.multipleSelection);
                    console.log(this.delList, '打印这个要删除的数组');
                    for (let i = 0; i < length; i++) {
                        str += this.multipleSelection[i].name + ' ';
                    }
                    this.$message.error(`删除了${str}`);
                    this.multipleSelection = [];
                })
                .catch(() => {});
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.settleData.prepaymentAmount = this.form.premium + this.form.moveCharges + this.form.freight;
            this.settleData.price = this.settleData.prepaymentAmount;
            this.settleData.amountReceivable = this.settleData.price - this.settleData.prepaymentAmount;

            this.settleData.waybillNumber = this.form.waybillNumber;
            this.settleData.freight = this.form.freight;
            this.settleData.rebates = this.form.rebates;
            this.settleData.premium = this.form.premium;
            this.settleData.moveCharges = this.form.moveCharges;

            this.settleData.settlementDate = this.form.settlementDate;
            this.settleData.refundType = this.form.refundType;

            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.settleData.refundType = this.form.refundType;
            console.log(this.settleData);
            // this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            console.log(this.form);
            this.settle();
            // this.getData();
            this.$set(this.tableData, this.idx, this.form);
        },
        //货运单结算操作
        settle() {
                console.log(this.settleData,"当前发送的对象");
            this.$axios.post('/api/settlement/business/change', this.settleData).then((res) => {
                if (res.data.code == 200) {
                    this.$message.success('业务变更成功');
                } else {
                    this.$message.error('业务变更失败');
                }
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.query.pageSize = val;
            this.getData();
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.query.currentPage = val;
            console.log();
            this.getData();
        },
        // 是否审核 是否结算
        stateFormat(row, column) {
            if (row.refundType === '0') {
                return '无业务';
            } else {
                return '单日结算';
            }
        },
        stateFormat1(row, column) {
            if (row.state === '1') {
                return '是';
            } else {
                return '否';
            }
        },
        stateFormat2(row, column) {
            if (row.state === '2') {
                return '是';
            } else {
                return '否';
            }
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-select {
    margin-left: 10px;
    width: 100px;
}
/* 按钮不展示悬浮鼠标效果 */
#nopoint {
    pointer-events: none;
}
/* 修改时间选择框长度 */
.handle-input {
    width: 150px;
    display: inline-block;
}
/* 搜索按钮间距设置 */
.select-button {
    margin-left: 10px;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
